#include "mainwindow.h"
#include <QApplication>
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
#include <logdialog.h>
#define DataBaseName "water.db"
static bool createDataBase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(DataBaseName);

    if(! db.open())
    {
        QMessageBox::critical(0,QString("错误")
                              ,QString("创建数据库失败\n原因：%1").arg(db.lastError().text()),
                              QMessageBox::Ok);
        return false;
    }
    return true;
}
static void createTable()
{
    QSqlQuery query;
    query.exec(QString("create table User(  UserName char(10) primary key, "
                       " password char(10) not null,userType char(20) not null  "
                       " );"));



    query.exec(QString("create table RealTimeData(id INTEGER  primary key AUTOINCREMENT not null,ph INTEGER,temperature  INTEGER,waterQuantity  INTEGER,WaterMargin  INTEGER,revtime datatime )"));
}
static void addDefaultData()
{
     QSqlQuery query;
    query.exec(" insert into User values('admin','admin','1')");
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    bool isExist = QFileInfo(DataBaseName).exists();
       if(! createDataBase())        //创建数据库
           return 1;
       if(! isExist)     //数据库如果不存在则需要添加默认数据
       {
           createTable();
           addDefaultData();      //创建默认数据
       }
      logDialog log;
       MainWindow w;
//      if(log.exec() == QDialog::Accepted)
//      {
//         log.getInfo(w.nowAccout,w.nowPassword,w.userType);
//         w.updateuserInfo();
//          w.show();
//      }
  w.show();
    return a.exec();
}
